package com.yiwenup.leetcode.top_interview_150;

import java.util.Stack;

/**
 * 文件描述
 *
 * @Project struct-algorithm
 * @Package com.yiwenup.leetcode.top_interview_150
 * @Author yiwenup
 * @Date 2024-12-24 22:11:25
 * @Description https://leetcode.cn/problems/min-stack/description/?envType=study-plan-v2&envId=top-interview-150
 */
public class No0155 {
    private final Stack<Integer> stack;
    private final Stack<Integer> tmp;
    private int min = Integer.MAX_VALUE;

    public No0155() {
        stack = new Stack<>();
        tmp = new Stack<>();
    }

    public void push(int val) {
        min = Math.min(min, val);
        stack.push(val);
    }

    public void pop() {
        stack.pop();
        min = Integer.MAX_VALUE;
        while (!stack.isEmpty()) {
            int val = stack.pop();
            min = Math.min(min, val);
            tmp.push(val);
        }
        while (!tmp.isEmpty()) {
            stack.push(tmp.pop());
        }
    }

    public int top() {
        return stack.peek();
    }

    public int getMin() {
        return min;
    }
}
